package hpu.bitenergy.servlet;

import hpu.bitenergy.Factory.Factory;
import hpu.bitenergy.IFactory.IFactory;
import hpu.bitenergy.List.GetList;
import hpu.bitenergy.util.QueryHelper;
import hpu.bitenergy.util.StringHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

//搜索框对应的服务，通过专家名和专家行业对专家的搜索
public class ssexpertlistServlet extends HttpServlet {

	public List<DBObject> listname;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		listname =new ArrayList<DBObject>();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String key=request.getParameter("key");
		System.out.println(key);
		
		
		//通过研究方向找专家名字
		IFactory research=Factory.GetIFactory("E");
		GetList li =research.GetListInstance();
		List<DBObject> expert_namelist =li.getQueryList(QueryHelper.getexpert_name(key));
		
		if(expert_namelist.size()==0)
			{//传入的不是关键字,通过专家名字找信息
				List<DBObject> list=li.getQueryList(QueryHelper.expert_name(StringHelper.AddTwoKong(key)));
				System.out.println(list.toString());
					if(list.size()!=0)
					{
					//有专家名数据
					request.setAttribute("list", list);
					request.getRequestDispatcher("/expert.jsp").forward(request, response);	
					}else{
						//有专家名数据
						list=null;
						request.setAttribute("listname",list);						
						request.getRequestDispatcher("/expertlist.jsp").forward(request, response);	
					}				
			}else{
			
				for(int i=0;i<expert_namelist.size();i++)
			{//通过专家名字集合进行查询信息
				List<DBObject> list=li.getQueryList(QueryHelper.expert_name(StringHelper.AddTwoKong(expert_namelist.get(i).get("expert_name").toString().trim())));				
				System.out.println(expert_namelist.get(i).get("expert_name").toString().trim());
				listname.addAll(list);	
			}
				System.out.println(listname.toString());			
				request.setAttribute("listname",listname);
				request.getRequestDispatcher("/expertlist.jsp").forward(request, response);	
			}	
			
			
	}

}
